package com.gitee.linzl.zuul.gateway.fiters;

import java.util.List;
import java.util.Optional;

import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;

import com.netflix.util.Pair;
import com.netflix.zuul.context.RequestContext;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public abstract class AbstractJSONPostFilter extends AbstractDefaultPostFilter {

	@Override
	public boolean shouldFilter() {
		RequestContext ctx = RequestContext.getCurrentContext();
		List<Pair<String, String>> headers = ctx.getZuulResponseHeaders();
		Optional<String> optional = headers.stream()
				.filter((pair) -> pair.first().equalsIgnoreCase(HttpHeaders.CONTENT_TYPE)).map(Pair::second)
				.findFirst();

		String contentType = optional.isPresent() ? optional.get() : null;
		return MediaType.APPLICATION_JSON_UTF8_VALUE.equalsIgnoreCase(contentType)
				|| MediaType.APPLICATION_JSON_VALUE.equalsIgnoreCase(contentType);
	}

}
